import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { FetchService } from '../../../core/net/fetch.service';
import { NzMessageService } from 'ng-zorro-antd';
import * as _ from 'lodash';
import {GeneralSettings} from "../GeneralSettings";
import {forkJoin} from "rxjs";
import {RegionMngModel, SearchMaintainService} from "../search-maintain.service";

@Component({
    selector: 'app-mt-mng-bureau',
    templateUrl: './mt-mng-bureau.component.html',
    styleUrls: ['./mt-mng-bureau.component.scss', '../search-maintain.component.scss']
})
export class MtMngBureauComponent implements OnInit {
    pageNum = 1;
    pageSize = 10;
    total: number;
    tableData = [];
    loading = true;
    searchForm: FormGroup;
    modalTitle: string = '';
    modalShow: boolean = false;
    detailForm: FormGroup;
    statOpt: any = {};
    props: any;
    commonSelection: RegionMngModel;     latestList: Array<any> = [];
    constructor(private fb: FormBuilder,
                private fetchService: FetchService,
                private message: NzMessageService,
                private searchService: SearchMaintainService,
                private params: GeneralSettings) {
        this.props = this.params;
    }

    ngOnInit() {
        this.searchForm = this.fb.group({
            manageName: ['']
        });
        this.detailForm = this.fb.group({
            id: [''],
            manageName: ['', Validators.required],
            manageCode: ['', Validators.required],
            county: ['', Validators.required],
            year: [''],
            irrigatedArea: [''],
            canalLength: [''],
            maxWaterDiversionCapacity: [''],
            annulWaterDiversionCapacity: [''],
            benefitDistrict: ['']
        });
        this.latestList = this.searchService.getLatestList('mtMngBureau');

        this.searchService.regionMngDataChange.subscribe(data => {
            this.commonSelection = {...data, id: data.stationId};
            this.getGridData();
        });
    }
    getLatestData(data): void {
        this.searchForm.patchValue({
            manageName: data.keyword
        });
        this.getGridData(true);
    }
    openEditModal(item?): void {
        this.modalTitle = item ? `编辑${item.manageName}` : '新增管理处';
        item ? this.detailForm.patchValue(item) : this.detailForm.reset();
        this.modalShow = true;
    }

    onDelete(id) {
        this.fetchService.get(`wrs/wrsBmanage/remove/${id}`).subscribe(resp => {
            this.message.info('删除成功');
            this.getGridData();
        });
    }

    onConfirm() {
        if (!this.detailForm.valid) {
            this.message.warning('信息填写有误');
            return;
        }
        const data = _.assign(this.detailForm.value, { type: 1 });
        data.stationId = '1';
        if (data.id) {
            this.fetchService.post('wrs/wrsBmanage/edit', data).subscribe(res => {
                this.message.success('修改成功');
                this.modalShow = false;
                this.getGridData();
            });
        } else {
            // data.stationId = "1";
            this.fetchService.post('wrs/wrsBmanage/add', data).subscribe(res => {
                this.message.success('新增成功');
                this.modalShow = false;
                this.getGridData();
            });
        }
    }

    onCancel() {
        this.modalShow = false;
    }

    getGridData(reset: boolean = false): void {
        if (reset) {
            this.pageNum = 1;
        }
        this.latestList = this.searchService.getLatestList('mtMngBureau');
        if (this.searchForm.value.manageName) {
            this.searchService.setLatestList('mtMngBureau', this.searchForm.value.manageName);
        }
        const params = _.assign(_.cloneDeep(this.commonSelection), _.cloneDeep(this.searchForm.value), {
            type: 1,
            pageNum: this.pageNum,
            pageSize: this.pageSize
        });
        delete params.id;
        const queryTableData = this.fetchService.post('wrs/wrsBmanage/list', params);
        const queryPanelData = this.fetchService.post('wrs/wrsBmanage/list', _.assign(_.cloneDeep(this.commonSelection), _.cloneDeep(this.searchForm.value), {
            type: 1,
            pageSize: 100000
        }));
        forkJoin(queryTableData, queryPanelData).subscribe(res => {
            this.loading = false;
            if (res) {
                this.total = res[0]['total'];
                this.tableData = res[0]['list'];
                const baseList = res[1]["list"];
                this.statOpt = {
                    statType: 'office',
                    baseList
                };
            }
        });
    }

}
